﻿using Supore.APP.UI.Controls.Notify;
using SuporeSliplampApp.Models;
using SuporeSliplampApp.Service;
using SuporeSliplampApp.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// UserSettingView.xaml 的交互逻辑
    /// </summary>
    public partial class UserSettingView : Window
    {
        AddUserService service = new AddUserService();
        LoginService loginService = new LoginService();
        private UserModel _user;
        private int modeindex = 0;

        public UserSettingView()
        {
            InitializeComponent();
            init();
            List<UserModel> users = service.GetUsers();
            userListBox.SelectedItem = null;
            userListBox.ItemsSource = users;
            userListBox.DisplayMemberPath = "UserName";
        }

        private void UpgradeUserList()
        {
            List<UserModel> users = service.GetUsers();
            userListBox.SelectedItem = null ;
            userListBox.ItemsSource = null;
            userListBox.ItemsSource = users;
            userListBox.DisplayMemberPath = "UserName";
        }


        private void init()
        {
            Username_now.Clear();
            Password_now.Clear();
            Password_set.Clear();
            Password_re.Clear();
            Username_now.IsEnabled = true;
            userinfo.Visibility = Visibility.Hidden;
            Psw_now.Visibility = Visibility.Visible;
            save.Visibility = Visibility.Hidden;
            concel.Visibility = Visibility.Hidden;
            New.Visibility = Visibility.Visible;
            del.Visibility = Visibility.Collapsed;
            modeindex = 0;
        }

        private void UserListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UserModel selectedUser = userListBox.SelectedItem as UserModel;
            if (userListBox.SelectedItem != null)
            {
                userinfo.Visibility = Visibility.Visible;
                Username_now.Text = selectedUser.UserName;
                isAdmin.IsOn = selectedUser.IsAdmin == 1;
                Username_now.IsEnabled = false;
                save.Visibility = Visibility.Visible;
                concel.Visibility = Visibility.Visible;
                del.Visibility = Visibility.Visible;
                New.Visibility = Visibility.Collapsed;
                modeindex = 2;
            }
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();

        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {

            init();
            Username_now.IsEnabled = true;
            Psw_now.Visibility= Visibility.Visible;
            del.Visibility = Visibility.Collapsed;
            New.Visibility = Visibility.Visible;
            Username_now.Clear();
            Password_now.Clear();
            Password_set.Clear();
            Password_re.Clear();
            isAdmin.IsOn = false;

            userListBox.SelectedItem = null;
        }

        private void NewUser_Click(object sender, RoutedEventArgs e)
        {
            if (modeindex == 1)
            {
                Username_now.Clear();
                Password_now.Clear();
                Password_set.Clear();
                Password_re.Clear();
                isAdmin.IsOn = false;
            }
            modeindex = 1;
            userinfo.Visibility = Visibility.Visible;
            Psw_now.Visibility = Visibility.Collapsed;
            save.Visibility = Visibility.Visible;
            concel.Visibility = Visibility.Visible;
        }

        private void Reset_Click(object sender, RoutedEventArgs e)
        {
            Username_now.Clear();
            Password_now.Clear();
            Password_set.Clear();
            Password_re.Clear();
            isAdmin.IsOn = false;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (modeindex == 1)
            {
                if (Password_set.Password.Trim() != Password_re.Password.Trim())
                {
                    if (Common.languageCode == "zh-CN")
                    {
                        MessageBox.Show("两次密码输入不一致，请重新输入!", "提醒");
                    }
                    else
                    {
                        MessageBox.Show("Passwords do not match. Please re-enter.!", "Tip");
                    }
                    //MessageBox.Show("两次密码输入不一致，请重新输入!", "提醒");
                    Password_set.Clear();
                    Password_re.Clear();
                    return;
                }

                _user = new UserModel();

                _user.UserName = this.Username_now.Text.Trim();
                _user.Password = this.Password_set.Password.Trim();

                if (this.isAdmin.IsOn)
                {
                    _user.IsAdmin = 1;
                }
                else
                {
                    _user.IsAdmin = 0;
                }

                if (!service.AddUser(_user))
                {
                    if (Common.languageCode == "zh-CN")
                    {
                        LayUINotify.ShowInfo("注册失败！");
                    }
                    else
                    {
                        LayUINotify.ShowInfo("Tip","Registration Failed!");
                    }
                    //LayUINotify.ShowInfo("注册失败！");
                    return;
                }
                else
                {
                    if (Common.languageCode == "zh-CN")
                    {
                        LayUINotify.ShowSuccess("注册成功！");
                    }
                    else
                    {
                        LayUINotify.ShowSuccess("Tip", "Registration Successful!");
                    }
                    //LayUINotify.ShowSuccess("注册成功！");
                    UpgradeUserList();
                    init();
                    return;
                }
            }

            if (modeindex == 2)
            {
                if (!loginService.CheckLogin(Username_now.Text.ToString(), Password_now.Password.ToString()))
                {
                    if (Common.languageCode == "zh-CN")
                    {
                        LayUINotify.ShowInfo("用户名或密码不正确");
                    }
                    else
                    {
                        LayUINotify.ShowInfo("Tip", "Username or Password Incorrect");
                    }
                    //LayUINotify.ShowInfo("用户名或密码不正确");
                    return;
                }
                else
                {
                    if (Password_set.Password.Trim() != Password_re.Password.Trim())
                    {
                        if (Common.languageCode == "zh-CN")
                        {
                            MessageBox.Show("两次密码输入不一致，请重新输入!", "提醒");
                        }
                        else
                        {
                            MessageBox.Show("Passwords do not match. Please re-enter!", "Tip");
                        }
                        //MessageBox.Show("两次密码输入不一致，请重新输入!", "提醒");
                        Password_set.Clear();
                        Password_re.Clear();
                        return;
                    }
                    _user = new UserModel();

                    _user.UserName = this.Username_now.Text.Trim();
                    _user.Password = this.Password_set.Password.Trim();

                    if (this.isAdmin.IsOn)
                    {
                        _user.IsAdmin = 1;
                    }
                    else
                    {
                        _user.IsAdmin = 0;
                    }

                    if (!service.UpdateUser(_user))
                    {
                        if (Common.languageCode == "zh-CN")
                        {
                            LayUINotify.ShowInfo("修改失败！");
                        }
                        else
                        {
                            LayUINotify.ShowInfo("Tip", "Modification Failed!");
                        }
                        //LayUINotify.ShowInfo("修改失败！");
                        return;
                    }
                    else
                    {
                        if (Common.languageCode == "zh-CN")
                        {
                            LayUINotify.ShowSuccess("修改成功！");
                        }
                        else
                        {
                            LayUINotify.ShowSuccess("Tip", "Modification Successful!");
                        }
                        //LayUINotify.ShowSuccess("修改成功！");
                        UpgradeUserList();
                        init();
                        return;
                    }
                }
            }
        }

        private void Refresh_Click(object sender, EventArgs e)
        {
            UpgradeUserList();
        }

        private void Del_Click(object sender, RoutedEventArgs e)
        {
            _user = new UserModel();
            _user.UserName = Username_now.Text.Trim();
            if (!service.DeleteUser(_user))
            {
                if (Common.languageCode == "zh-CN")
                {
                    LayUINotify.ShowInfo("删除失败！");
                }
                else
                {
                    LayUINotify.ShowInfo("Tip", "Deletion Failed!");
                }
                //LayUINotify.ShowInfo("删除失败！");
                return;
            }
            else
            {
                if (Common.languageCode == "zh-CN")
                {
                    LayUINotify.ShowInfo("删除成功！");
                }
                else
                {
                    LayUINotify.ShowInfo("Tip", "Deletion Successful!");
                }
                //LayUINotify.ShowInfo("删除成功！");
                UpgradeUserList();
                init();
                return;
            }

        }
    }
}
